package com.vpx.server.mapper.system;

import com.vpx.server.bean.entity.*;
import com.vpx.server.bean.info.system.MenuOperateInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author lihui
 * @title: vpx
 * @module TODO
 * @description: TODO
 * @date 2021/9/11 18:06
 */
public interface AuthorityMapper  {

	/**
	 * 查询用户
	 * @param loginAccount
	 * @return
	 */
    @Select("select * from sys_user where login_account = #{loginAccount}" )
    SysUser queryUserByLoginNum(@Param("loginAccount") String loginAccount);

    /**
     * 查询用户角色
     * @param userId
     * @return
     */
    @Select("select r.* from sys_user_role ur inner join sys_role r on ur.role_id = r.id where ur.user_id = #{userId}" )
    List<SysRole> queryUserRoles(@Param("userId") Long userId);

    /**
     * 查询用户权限
     * @param roleId
     * @return
     */
    @Select("select mo.* from sys_menu_operate mo inner join sys_role_auth ra on mo.id = ra.operator_id" +
            " where ra.role_id = #{roleId}" )
    List<SysMenuOperate> queryUserAuth(@Param("roleId") Long roleId);


    /**
     * 查询有权限的菜单
     * @param roleIdList
     * @return
     */
    @Select("<script>" +
            "select DISTINCT m.id, m.menu_name,m.parent_id,m.icon,m.code, m.router, m.component,m.sort_num " +
            "from sys_role_auth a " +
            "inner join sys_menu m on a.menu_id = m.id " +
            "<where>" +
            "   <if test=\"roleIdList != null and roleIdList.size > 0\">" +
            "     and a.role_id in " +
            "     <foreach collection='roleIdList' item='item' index='index' separator=',' open='(' close=')'> " +
            "           #{item}" +
            "     </foreach>" +
            "   </if>  " +
            "</where>" +
            "</script>" )
    List<SysMenu> queryAuthMenu(@Param("roleIdList") List<Long> roleIdList);

    /**
     * 查询有权限的按钮
     * @param roleIdList
     * @return
     */
    @Select("<script>" +
            "select DISTINCT mo.* " +
            "from sys_role_auth a " +
            "inner join sys_menu_operate mo on a.operator_id = mo.id " +
            "<where>" +
            "   <if test=\"roleIdList != null and roleIdList.size > 0\">" +
            "     and a.role_id in " +
            "     <foreach collection='roleIdList' item='item' index='index' separator=',' open='(' close=')'> " +
            "           #{item}" +
            "     </foreach>" +
            "   </if>  " +
            "</where>" +
            "</script>" )
    List<SysMenuOperate> queryAuthOperate(@Param("roleIdList") List<Long> roleIdList);

    /**
     * 查询所有的菜单
     * @return
     */
    @Select("select * from sys_menu m order by sort_num")
    List<SysMenu> queryAllMenu();

    /**
     * 查询所用功能点
     * @return
     */
    @Select("select * from sys_menu_operate m ")
    List<MenuOperateInfo> queryAllOperator();


    /**
     * select m.id as menu_id,m.menu_name,m.parent_id,m.icon,m.code, m.router, m.component,m.sort_num ,
     * mo.id as operator_id, mo.operate_name, mo.icon,mo.code, mo.operate_url
     * from sys_role_auth a
     * inner join sys_menu_operate mo on a.operator_id = mo.id
     * inner join sys_menu m on a.menu_id = m.id
     */
}
